package org.example;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.experimental.Accessors;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.junit.Test;

import java.io.*;
import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName Test
 * @Description TODO
 * @Author 肖翔
 * @Date 2020/6/16 17:20
 * @Version 1.0
 **/
public class Test1 {

    private Map<String, Map<String, String>> read(String path) {
        Map<String, Map<String, String>> map = new HashMap<>(10000);
        InputStream inp = null;
        Workbook wb = null;
        try {
            inp = new FileInputStream(path);
            wb = WorkbookFactory.create(inp);
            //获取第一个sheet
            Sheet sheet = wb.getSheetAt(0);
            int size = sheet.getLastRowNum();
            for (int i = 1; i <= size; i++) {
                Row row = sheet.getRow(i);
                String key = getCellValue(row, 0);
                String value = getCellValue(row, 1);
                if (value.length() < 12) {
                    value = StringUtils.rightPad(value, 12, "0");
                }
                String area = getCellValue(row, 2);
                Map<String, String> valueMap = new HashMap<>(2);
                valueMap.put("code", value);
                valueMap.put("area", area);
                while (map.containsKey(key)) {
                    key += 1;
                }
                map.put(key, valueMap);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                inp.close();
                wb.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return map;
    }

    private String getCellValue(Row row, int num) {
        String value = null;
        Cell cell = row.getCell(num);
        if (cell != null) {
            if (cell.getCellType() == CellType.NUMERIC) {
                double numericCellValue = cell.getNumericCellValue();
                value = NumberFormat.getInstance().format(numericCellValue).replaceAll(",", "");
            } else {
                value = cell.getStringCellValue();
            }
        } else {
            value = "";
        }
        return value;
    }

    private void output(List<Bean> list) {
        String outputPath = "C:\\Users\\Administrator\\Desktop\\output.xlsx";
        XSSFWorkbook wb = new XSSFWorkbook();
        XSSFSheet result = wb.createSheet("结果");
        XSSFRow titleRow = result.createRow(0);
        titleRow.createCell(0).setCellValue("aArea");
        titleRow.createCell(1).setCellValue("aName");
        titleRow.createCell(2).setCellValue("aCode");
        titleRow.createCell(3).setCellValue("bCode");
        titleRow.createCell(4).setCellValue("bName");
        titleRow.createCell(5).setCellValue("bArea");

        list.stream().forEach(bean -> {
            wirteRow(result, bean);
        });
        OutputStream ous = null;
        try {
            ous = new FileOutputStream(outputPath);
            wb.write(ous);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                ous.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                wb.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }

    private void wirteRow(XSSFSheet result, Bean bean) {
        XSSFRow row = result.createRow(bean.index);
        row.createCell(0).setCellValue(bean.aAreaName);
        row.createCell(1).setCellValue(bean.aName);
        row.createCell(2).setCellValue(bean.aCode);
        row.createCell(3).setCellValue(bean.bCode);
        row.createCell(4).setCellValue(bean.bName);
        row.createCell(5).setCellValue(bean.bAreaName);
    }

    @Test
    public void test() {
        Map<String, Map<String, String>> map1 = read("C:\\Users\\Administrator\\Desktop\\惠州行政区划.xlsx");
        Map<String, Map<String, String>> map2 = read("C:\\Users\\Administrator\\Desktop\\111.xlsx");
        List<Bean> list = new ArrayList<>(10000);
        map2.forEach((k, valueMap) -> {
            String bCode = valueMap.get("code");
            String bArea = valueMap.get("area");

            //先统统去1
            k = k.replace("1", "");

            List<String> areaList = new ArrayList<>();
            List<String> codeList = new ArrayList<>();

            //map1中找到所有的
            for (String tmp = k; map1.containsKey(tmp); tmp += 1) {
                Map<String, String> map = map1.get(tmp);
                codeList.add(map.get("code"));
                areaList.add(map.get("area"));
            }

            if (!codeList.isEmpty() && !codeList.contains(bCode)) {
                for (int i = 0; i < codeList.size(); i++) {
                    list.add(new Bean(0, areaList.get(i), k, codeList.get(i), bArea, k, bCode));
                }
            }

            //统一处理索引
            int i = 1;
            for (Bean bean : list) {
                bean.setIndex(i++);
            }
        });
        output(list);
    }

    @Data
    @AllArgsConstructor
    @Accessors(chain = true)
    private class Bean {
        private int index;
        private String aAreaName;
        private String aName;
        private String aCode;
        private String bAreaName;
        private String bName;
        private String bCode;
    }

}
